package com.example.lanouhn.bantangapp.activities.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FragmentSearchListleft;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FragmentSerachRecyclerviewRight;
import com.example.lanouhn.bantangapp.activities.contants.Search;
import com.example.lanouhn.bantangapp.activities.contants.Search_product;
import com.example.lanouhn.bantangapp.activities.utils.HttpUtils;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_Search_product;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class Fragment_Search_SingleProduct extends Fragment {

    private ListView listviewleft;//左边的商品列表
    private RecyclerView recyclerView;//右边的商品展示
    private List<Search_product> listSearch = new ArrayList<>();//实体类集合
    private Adapter_FragmentSearchListleft adapter_fragmentSearchListleft;//listview的adapter
    private Adapter_FragmentSerachRecyclerviewRight adapter_fragmentSerachRecyclerviewRight;//recyclerviewadapter

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item_list, container, false);
        listviewleft = (ListView) view.findViewById(R.id.listleft);
        //去掉listview的下划线
        listviewleft.setDivider(null);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);


        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDataToPost();
    }

    //post解析数据
    private void initDataToPost() {

        new Thread(new Runnable() {
            @Override
            public void run() {

                String url = Search.SEARCHURL;//获取网络地址

                String params = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2425199&oauth_token=dc743c52935a58a1391eab89d551e75c&track_deviceid=868596020655823&track_device_info=m2+note&channel_name=UMENG_CHANNEL&app_installtime=1468811189&app_versions=5.8.4&os_versions=5.1&screensize=1080&v=16&is_new=1";


                String result = HttpUtils.doPost(url,params);

                if (null!=result&&result.length()>0){
                    listSearch = JsonUtils_Search_product.getsrachList(result);
                    handler.sendEmptyMessage(1);
                }else{
                    handler.sendEmptyMessage(0);
                }

            }
        }).start();
    }

    //验证数据的线程
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    //失败
                    Toast.makeText(getActivity(), R.string.fail, Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    //成功
                    //绑定Adapter并在listview上展示
                    adapter_fragmentSearchListleft = new Adapter_FragmentSearchListleft(listSearch, getActivity());
                    listviewleft.setAdapter(adapter_fragmentSearchListleft);


                    //点击Item变换字体颜色和背景颜色
                    listviewleft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            //改变颜色
                            adapter_fragmentSearchListleft.setSelectPosition(position);
                            //改变右边Re的内容
                            adapter_fragmentSerachRecyclerviewRight =
                                    new Adapter_FragmentSerachRecyclerviewRight(getActivity(), listSearch.get(position).getSubclassList());
                            recyclerView.setAdapter(adapter_fragmentSerachRecyclerviewRight);
                            StaggeredGridLayoutManager staggeredGridLayoutManager =
                                    new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL);
                            recyclerView.setLayoutManager(staggeredGridLayoutManager);

                        }
                    });

                    //设置默认选中
                    listviewleft.setSelection(0);
                    //设置默认右边Re的内容
                    adapter_fragmentSerachRecyclerviewRight = new Adapter_FragmentSerachRecyclerviewRight(getActivity(), listSearch.get(0).getSubclassList());
                    recyclerView.setAdapter(adapter_fragmentSerachRecyclerviewRight);
                    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(staggeredGridLayoutManager);
                    break;
            }
        }
    };
}



